package com.fanxin.controller;

import com.fanxin.entities.Distribute;
import com.fanxin.entities.DistributeVO;
import com.fanxin.entities.ResultData;
import com.fanxin.entities.User;
import com.fanxin.mapper.DistributeMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.ArrayList;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DistributeMapper mapper;

    @RequestMapping("login_page")
    public String login(String username,String password){
        return "login";
    }
    /**
     * 登录接口
     * 不同的异常表示不同的情况
     * 1、ConcurrentAccessException 并发访问异常（多个用户同时登录时抛出）
     * 2、UnknownAccountException 未知的账号
     * 3、ExcessiveAttemptsException 认证次数超过限制
     * 4、DisabledAccountException 禁用的账号
     * 5、LockedAccountException 账号被锁定
     * 6、pportedTokenException 使用了不支持的Token
     * @return
     */
    @RequestMapping("login")
    @ResponseBody
    public String login(User user){
        if("".equals(user.getUsername()) || null == user.getUsername() || "".equals(user.getPassword()) || null == user.getPassword()){
            return "用户名或密码不能为空";
        }else {
            //创建subject
            Subject subject = SecurityUtils.getSubject();
            try {
                UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),user.getPassword());
                subject.login(token);
                Session session = subject.getSession();
                session.setAttribute("user",new User(user.getUsername(),user.getPassword()));
                return "登录成功";
            }catch (Exception e){
                e.printStackTrace();
                return "登录失败";
            }
        }
    }


    @RequestMapping("query_distribute")
    @ResponseBody
    public List<DistributeVO> distribute(){
        List<Distribute> distributes=mapper.queryDistribute();
        List<DistributeVO> rs=new ArrayList<>();
        for(Distribute distribute:distributes){
            rs.add(new DistributeVO(distribute.getCount(),distribute.getStage()));
        }
        return rs;
    }



    @RequestMapping("query_distribute_1")
    @ResponseBody
    public ResultData distribute_1(){
        List<Distribute> distributes=mapper.queryDistribute();
        return new ResultData(0,"成功",100,distributes);
    }

    @RequestMapping("query_distribute_2")
    @ResponseBody
    public List<Distribute> distribute_2(){
        return mapper.queryDistribute();
    }

    @RequestMapping("query_distribute_ById")
    @ResponseBody
    public Distribute distribute_ById(){
        return mapper.queryDistributeById(1);
    }

    @RequestMapping(value = "query_student_by_id",method = RequestMethod.GET)
    @ResponseBody
    public Distribute queryStudentByName(@RequestParam("id") int id){
        return mapper.queryDistributeById(id);
    }


    @RequestMapping(value = "query_student_by_id_use_post",method = RequestMethod.POST)
    @ResponseBody
    public Distribute queryPost(@RequestParam(value = "id",required = false) int id){
        return mapper.queryDistributeById(id);
    }

    @RequestMapping(value = "query_student_by_id_use_post1",method = RequestMethod.POST)
    @ResponseBody
    public Distribute queryPost1(){
        return mapper.queryDistributeById(1);
    }

    @RequestMapping("redis_test")
    @ResponseBody
    public String redisTest(){
        ValueOperations valueOperations=redisTemplate.opsForValue();
        valueOperations.set("username","fanxin");
        return valueOperations.get("username").toString();
    }


    @RequestMapping("home")
    public String home(){
        return "home";
    }


    @RequestMapping("layui")
    public String layui(){
        return "layui";
    }


    @RequestMapping("notice")
    public String notice(){
        return "notice";
    }

}
